package com.login.app.controller;
import com.login.app.bean.User;
import com.login.app.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
//@RequestMapping("/User")
public class LoginController {

    Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Qualifier("userServiceImpl")
    @Autowired
    private UserService userService;
//
//    @GetMapping("/Home")
//    public String toHome(){
//        return "fore/homePage";
//    }
//
//    @GetMapping("/toLogin")
//    public String toLogin(){
//        return "fore/loginPage";
//    }

    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public User login(@RequestParam String username, @RequestParam String password,
                        HttpSession session){
        logger.debug("用户验证登录");
        User user = userService.login(username, password);
        if (user == null) {
            return null;
        } else {
            session.setAttribute("user", user);
            return user;
        }
    }

    /**
     * 退出
     * @param session
     * @return
     */
    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "";
    }


//    @GetMapping("/toLogin")
//    public String toLogin(){
//        return "fore/loginPage";
//    }

//    //登陆验证-ajax
//    @ResponseBody
//    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
//    public String Login(HttpSession session, @RequestParam String username, @RequestParam String password){
//        User user = userService.login(username, password);
//        JSONObject jsonObject = new JSONObject();
//        if (user == null) {
//            jsonObject.put("success", false);
//            return "登录失败!";
//        } else {
//            session.setAttribute("user", user);
//            jsonObject.put("success", true);
//            return "登陆成功";
//        }
//    }




}
